<?php
declare (strict_types=1);

namespace app\common\service;

use app\common\model\RegionListModel;
use think\facade\Cache;

class RegionService
{

    private $region_key = 'REGION_INFO';

    /*
     * @title 提前数组中的code区域编码 - 主要提供后台搜索区域使用
     * @param array $param 带区域信息的数组
     * @return string
     * */
    public function getAreaCode(array $param): string
    {
        if (empty($param)) {
            return '0'; //全国
        }
        if (!empty($param['area'])) { //区
            return $param['area'];
        }

        if (!empty($param['city'])) { //省
            return $param['city'];
        }

        if (!empty($param['province'])) { //市
            return $param['province'];
        }
        return '0';
    }

    /*
     * @title 将区域信息写入到redis缓存中
     * @return bool
     * */
    public function setRedisCache(bool $all = true): bool
    {
        if (!$all) {
            Cache::store('redis')->delete('all_map_json_data');
            Cache::store('redis')->delete('region_all_lev');
            return false;
        }
        $start = 0;
        $limit = 2000;
        while (1) {
            $list = RegionListModel::field('region_code,region_name')->limit($start, $limit)->select();
            if ($list->isEmpty()) {
                break;
            }
            foreach ($list as $value) {
                Cache::store('redis')->set($this->region_key . $value->region_code, $value->region_name, 0);
            }
            $start += $limit;
        }
        Cache::store('redis')->set($this->region_key . 'cache_status', 1, 0);
        return true;
    }

    /*
     * @title 根据地区编码获取-地区名称
     * @param string $code 区域编码
     * @return string
     * */
    public function getAreaName(string $code): string
    {
        if (!Cache::store('redis')->get($this->region_key . 'cache_status')) {
            $this->setRedisCache();
        }
        $len = strlen($code);
        if ($len < 2 || $code == 0) {
            return '全国';
        }

        if ($len > 6) {
            $len = 6;
            $code = substr($code, 0, 6);
        }

        $name = '';

        switch ($len) {
            case 6:
                $name = Cache::store('redis')->get($this->region_key . substr($code, 0, 2));
                $name .= '-' . Cache::store('redis')->get($this->region_key . substr($code, 0, 4));
                $name .= '-' . Cache::store('redis')->get($this->region_key . $code);
                break;
            case 4:
                $name = Cache::store('redis')->get($this->region_key . substr($code, 0, 2));
                $name .= '-' . Cache::store('redis')->get($this->region_key . $code);
                break;
            case 2:
                $name = Cache::store('redis')->get($this->region_key . $code);
                break;
        }
        return $name;
    }

    /*
     * @title 获取js使用的json数据 - 后台城市3级xcity.js联动使用的js - 需要手动生成,然后写入到js文件中
     * @return array
     * */
    public function mapJson(): array
    {
        $key = 'all_map_json_data';
        $res = Cache::store('redis')->get($key);
        if (!empty($res) && is_array($res)) {
            return $res;
        }

        $list = RegionListModel::where('pid', '=', '0')->select();
        $data = [
            [
                'name' => '全国',
                'code' => '0',
                'cityList' => [
                    [
                        'name' => '全国',
                        'code' => '0',
                        'areaList' => [
                            [
                                'name' => '全国',
                                'code' => 0
                            ]
                        ]
                    ]
                ],
            ]
        ];
        foreach ($list as $value1) {
            $data1 = [
                'name' => $value1->region_name,
                'code' => $value1->region_code,
                'cityList' => [
                    ['name' => '请选择', 'code' => '0']
                ]
            ];
            $list2 = RegionListModel::where('pid', '=', $value1->region_code)->select();
            foreach ($list2 as $value2) {
                $data2 = [
                    'name' => $value2->region_name,
                    'code' => $value2->region_code,
                    'areaList' => [
                        ['name' => '请选择', 'code' => '0']
                    ]
                ];
                $list3 = RegionListModel::where('pid', '=', $value2->region_code)->select();
                foreach ($list3 as $value3) {
                    $data3 = [
                        'name' => $value3->region_name,
                        'code' => $value3->region_code,
                    ];
                    $data2['areaList'][] = $data3;
                }
                $data1['cityList'][] = $data2;
            }
            $data[] = $data1;
        }
        Cache::store('redis')->set($key, $data, 0);
        return $data;
    }

    /*
     * @title 根据用户请求的数据 获取 地区编码
     * @param string $code
     * @return array
     * */
    function getAreaInfo(string $code): array
    {
        $len = strlen($code);
        $data = [
            'province' => '',
            'city' => '',
            'area' => '',
        ];
        switch ($len) {
            case 6:
                $data['province'] = substr($code, 0, 2);
                $data['city'] = substr($code, 0, 4);
                $data['area'] = $code;
                break;
            case 4:
                $data['province'] = substr($code, 0, 2);
                $data['city'] = $code;
                break;
            case 2:
                $data['province'] = $code;
                break;
        }
        return $data;
    }

    /*
     * @title 获取全部的菜单,安装等级
     * */
    public function getAllLev(): array
    {
        $cache_key = 'region_all_lev';
        $list = Cache::store('redis')->get($cache_key);
        if (!$list) {
            $field = '*';
            $list = [];
            $data = RegionListModel::field($field)->where('pid', '=', 0)->order('px asc')->select()->toArray();
            foreach ($data as $key => $item) {
                $item['mark'] = '|-';
                $list[] = $item;
                $list1 = RegionListModel::field($field)->where('pid', '=', $item['region_code'])->order('px asc')->select()->toArray();
                foreach ($list1 as $k => $v) {
                    $v['mark'] = '|------';
                    $list[] = $v;
                }
            }
            Cache::store('redis')->set($cache_key, $list, 0);
        }
        return $list;
    }

}

